/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.manager;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getBodies;

import java.util.List;

import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.OpaqueExpression;
import org.eclipse.uml2.uml.StateInvariant;
import org.eclipse.uml2.uml.ValueSpecification;

import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Direct edit manager for StateInvariants
 * 
 * @author frankd@ifi.uio.no
 */
public class StateInvariantEditManager extends LabelDirectEditManager {

    public StateInvariantEditManager(GraphicalEditPart source, CellEditorLocator locator) {
        super(source, locator);
    }

    @SuppressWarnings("unchecked")
    protected String getInitialEditorText() {
        String initialValue = null;

        final GraphElement view = (GraphElement) getEditPart().getModel();
        final StateInvariant stateInv = ModelUtil.<StateInvariant> getSemantic(view);
        final Constraint invariant = stateInv.getInvariant();
        if (invariant != null) {
            final ValueSpecification valueSpec = invariant.getSpecification();
            if (valueSpec != null && valueSpec instanceof OpaqueExpression) {
                List<String> bodies = getBodies((OpaqueExpression) valueSpec);
                if (!bodies.isEmpty())
                    initialValue = bodies.get(0);
            }
        }

        assert initialValue != null;
        return initialValue;
    }
}
